package com.xli.organ.role.framerole.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xli.dto.component.model.TableModel;
import com.xli.dto.controller.BaseController;
import com.xli.dto.params.IdsDTO;
import com.xli.dto.result.ResultVO;
import com.xli.dto.result.status.Status;
import com.xli.dto.validation.group.IGroup;
import com.xli.organ.role.framerole.entity.FrameRole;
import com.xli.organ.role.framerole.entity.dto.RoleAddDTO;
import com.xli.organ.role.framerole.entity.dto.RoleSearchDTO;
import com.xli.organ.role.framerole.entity.dto.RoleUpdateDTO;
import com.xli.organ.role.framerole.entity.mapper.IFrameRoleMapper;
import com.xli.organ.role.framerole.entity.vo.RoleVO;
import com.xli.organ.role.framerole.service.IFrameRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "角色管理")
@RestController
@RequestMapping("/frameRole")
public class FrameRoleController extends BaseController {

    @Autowired
    private IFrameRoleService iFrameRoleService;

    @Operation(summary = "保存角色")
    @PostMapping(value = "/add")
    public ResultVO<String> add(@RequestBody @Validated(IGroup.add.class) RoleAddDTO dto) {
        FrameRole frameRole = iFrameRoleService.findByName(dto.getRoleName());
        if (frameRole == null) {
            frameRole = IFrameRoleMapper.INSTANCE.toEntity(dto);
            frameRole.setId(IdUtil.getSnowflakeNextId());
            if (iFrameRoleService.insert(frameRole)) {
                return new ResultVO<>(Status.SUCCESS, "保存成功");
            }
            return new ResultVO<>(Status.FAILED, "保存失败");
        }
        return new ResultVO<>(Status.FAILED, "保存失败，角色名已存在");
    }

    @Operation(summary = "删除角色")
    @PostMapping(value = "/delete")
    public ResultVO<String> delete(@RequestBody @Validated(IGroup.delete.class) IdsDTO dto) {
        int i = 0;
        for (String id : dto.getIds()) {
            if (iFrameRoleService.delete(Long.parseLong(id))) {
                i++;
            }
        }
        return new ResultVO<>(Status.SUCCESS, i + "条数据删除成功");
    }

    @Operation(summary = "更新角色")
    @PostMapping(value = "/update")
    public ResultVO<String> update(@RequestBody @Validated(IGroup.update.class) RoleUpdateDTO dto) {
        FrameRole frameRole = iFrameRoleService.findByName(dto.getRoleName());
        if (frameRole == null || dto.getRoleName().equals(frameRole.getRole_name())) {
            if (iFrameRoleService.update(IFrameRoleMapper.INSTANCE.toEntity(dto))) {
                return new ResultVO<>(Status.SUCCESS, "更新成功");
            }
            return new ResultVO<>(Status.FAILED, "更新失败");
        }
        return new ResultVO<>(Status.FAILED, "更新失败，角色名已存在");
    }

    @Operation(summary = "查询详情")
    @PostMapping(value = "/detail/{id}")
    public ResultVO<RoleVO> detail(@PathVariable("id") @NotBlank(message = "id必填") String id) {
        FrameRole frameRole = iFrameRoleService.find(Long.valueOf(id));
        if (frameRole != null) {
            return new ResultVO<>(Status.SUCCESS, "查询成功", IFrameRoleMapper.INSTANCE.toVO(frameRole));
        }
        return new ResultVO<>(Status.FAILED, "查询失败");
    }

    @Operation(summary = "查询列表")
    @PostMapping(value = "/search")
    public ResultVO<TableModel<RoleVO, FrameRole, RoleSearchDTO>> search(@RequestBody @Validated(IGroup.search.class) RoleSearchDTO dto) {

        TableModel<RoleVO, FrameRole, RoleSearchDTO> tableModel = new TableModel<>(dto) {
            @Override
            public List<RoleVO> fetch(int current, int pageSize) {
                Page<FrameRole> page = iFrameRoleService.findList(getQueryWrapper(), current, pageSize);
                this.setTotal(page.getTotal());
                return page.convert(IFrameRoleMapper.INSTANCE::toVO).getRecords();
            }
        };
        return new ResultVO<>(Status.SUCCESS, "查询成功", tableModel);
    }
}
